/**
 * 文件相关接口
 */
// import FileSaver from 'file-saver';
import http, { BASE_NAME } from './index';

export const mapServerBasePath = `${BASE_NAME}/api/map_service/relation`;

/**
 *根据模块id查询已发布的列表
 *
 * @export
 * @param {{menuCode:string}} data
 * @return {Promise<[any,any]>}
 */
export async function getRelationById(data) {
  try {
    const response = await http.get(mapServerBasePath, {
      params: data,
    });

    return [null, response.data];
  } catch (e) {
    return [(typeof e === 'string' && e) || 'error', null];
  }
}

/**
 *图层根据shp切割
 *
 * @export
 * @param {{
 * serviceId:string; // 切割图层
 * shp:File; // 服务id
 * layerName:string; // 图层名称
 * }} data
 * @return {Promise<[any,any]>}
 */
export async function cuttingToShp(data) {
  try {
    const formData = new FormData();

    for (const key in data) {
      if (Object.hasOwnProperty.call(data, key)) {
        const element = data[key];

        formData.append(key, element);
      }
    }

    const response = await http.post(`${BASE_NAME}/api/map_service/clip`, formData, {
      responseType: 'blob',
    });

    return [null, response];
  } catch (e) {
    return [e, null];
  }
}
